var sideMenuWidth = 0;
var viewportWidth = 0;
var currentPage, currentPosition = 0, topLinkWidth = 0;
var initialViewportSize = 640;
var sidemenuIconSize = 0, topMenuPadding = 80, placeBoxHeight = 559;

jQuery(document).ready(function(){
   
    
    //on focus change bg of input
    jQuery('#registerMe .regContent form .field input').focus(function(){
        jQuery(this).parent().addClass('focus');    
    });
    jQuery('#registerMe .regContent form .field input').focusout(function(){
        jQuery(this).parent().removeClass('focus');   
    });
    
    //cutom checkbox
    jQuery('#registerMe .regContent form .agreeChkbox input').css('opacity', 0);
    jQuery('#registerMe .regContent form .agreeChkbox input').click(function(){
        jQuery(this).parent().toggleClass('tick');
    });
    
    //sliding script
    
        //on load move div to out of the screen axcept current and header.
        widthCalculation();
        currentPage = jQuery('.page.active').attr('page');
        jQuery('.sideMenu').css('left', -sideMenuWidth);
        adjustElements();
                
        
        //sideMenu sliding effect when click on mainMenuLink
        jQuery('.mainMenuLink').click(function(){
           // widthCalculation();
            console.log('currentPosition: '+currentPosition);
            console.log('sideMenuWidth: '+sideMenuWidth);
            console.log('currentPosition+sideMenuWidth: '+currentPosition+sideMenuWidth);
            
            if(!jQuery('.sideMenu').hasClass('active')){
                jQuery('.sideMenu').stop().animate({left: 0}, function(){
                    jQuery('.sideMenu').toggleClass('active');
                    jQuery('.topLinks').css('visibility', 'hidden');
                });
                jQuery('.content').stop().animate({left: currentPosition+sideMenuWidth});
                jQuery('#header').stop().animate({left: sideMenuWidth});
            }else{
                jQuery('.sideMenu').stop().animate({left: -sideMenuWidth}, function(){
                    jQuery('.sideMenu').toggleClass('active');
                });
                jQuery('.topLinks').css('visibility', 'visible')
                jQuery('.content').stop().animate({left: currentPosition});
                jQuery('#header').stop().animate({left: 0});
            }
			return false;
        });
        
        //page slide on click
        jQuery('.topLinks ul li a').click(function(){
            pageNumber = jQuery(this).attr('target_page');
            currentItem = jQuery(this);
            currentPosition = -((pageNumber-1)*viewportWidth);
            
            if(!jQuery('.page'+pageNumber).hasClass('active')){
                if(jQuery('.sideMenu').hasClass('active')){
                        jQuery('.sideMenu').stop().animate({left: -sideMenuWidth}, function(){
                        jQuery('.sideMenu').toggleClass('active');
                    });
                }
                
                jQuery('.content').animate({left: currentPosition}, function(){
                    jQuery('.page.active').removeClass('active');
                    jQuery('#header .topLinks ul li').removeClass('active');
                    jQuery(currentItem).parent().addClass('active');
                    jQuery('.page'+pageNumber).addClass('active');
                    currentPage = jQuery('.page.active').attr('page');
                    currentPosition = -((pageNumber-1)*viewportWidth);
                    console.log('currentPosition: '+currentPosition);
                });
            }
        }); 
    
        //on resize adjust width
        jQuery(window).resize(function(){
            widthCalculation();
            setPosition();
            adjustElements();
            if(viewportWidth <= 582){
                jQuery('.sideMenu .menu ul li').css('background-size', 'auto 60px');
            }else if(viewportWidth <= 465){
                jQuery('.sideMenu .menu ul li').css('background-size', 'auto 40px');
            }else{
                jQuery('.sideMenu .menu ul li').css('background-size', '');
            }
        });
        
        
        
        //functions        
        function widthCalculation(){
            sideMenuWidth = jQuery('.sideMenu').width();
            viewportWidth = jQuery(window).width();
            topLinkWidth = 0;
            jQuery('.topLinks ul li').each(function(){
                topLinkWidth += jQuery(this).outerWidth();
            });
            jQuery('.page').width(viewportWidth);
            jQuery('.content').width(viewportWidth*(jQuery('.page').length));
            jQuery('#header .topLinks ul').width(topLinkWidth);
            jQuery('#header .topLinks').width(viewportWidth-(jQuery('.mainMenuLink').outerWidth(true))-3);
            console.log(jQuery('.page').length);
            console.log('document width '+viewportWidth);
        }
    
        function setPosition(){
            currentPosition = -((currentPage-1)*viewportWidth);
            jQuery('.content').css('left', currentPosition);
            
            if(!jQuery('.sideMenu').hasClass('active')){
                jQuery('.sideMenu').css('left', -sideMenuWidth);
                jQuery('#header').css('left', 0);
            }else{
                jQuery('.sideMenu').css('left', 0);
                jQuery('#header').css('left', sideMenuWidth);
            }
            
        }
    
        function resizeFontSize(target, fontSize){
            var newFontSize = (fontSize/initialViewportSize)*viewportWidth;
            
            if(newFontSize < fontSize){
                jQuery(target).css('font-size', newFontSize);
            }else{
                jQuery(target).css('font-size', '');
            }
        }
    
        function adjustElements(){
            
            if(viewportWidth < initialViewportSize){
                sidemenuIconSize = (57/initialViewportSize) * viewportWidth;
                placeBoxConHeight = jQuery('.placeBox .placeContent').height();
                jQuery('.sideMenu .menu ul li a span.icon img').css({'width':sidemenuIconSize, 'height':'auto'});
                console.log('placecontent ' +placeBoxConHeight);
                console.log('placeBoxHeight ' + placeBoxHeight);
                console.log('final ' + (placeBoxHeight-placeBoxConHeight-28));
                jQuery('.placeBox .placeContent').css({'margin-top':(placeBoxHeight-placeBoxConHeight-28)});
            }else{
                jQuery('.sideMenu .menu ul li a span.icon img').css({'width':'', 'height':''});
                jQuery('.placeBox .placeContent').css({'margin-top':''});
            }
            
            resizeFontSize('h2', 60);
            resizeFontSize('.placeBox .placeContent .colLeft span.firstTxt', 20);
            resizeFontSize('.placeBox .placeContent .colLeft span.secondTxt', 18);
            resizeFontSize('.placeBox .moreBtn', 21);
            resizeFontSize('.sideMenu .profileDetail .title', 46);
            resizeFontSize('.sideMenu .profileDetail .postion, .sideMenu .profileDetail .postion .value', 26);
            resizeFontSize('.sideMenu .profileDetail .level', 28);
            resizeFontSize('.sideMenu .menu ul li a', 24);
            //resizeFontSize('#header .topLinks ul li', 22);
        }

        
});

jQuery(window).load(function(){
    if(viewportWidth < initialViewportSize){
        sidemenuIconSize = (57/initialViewportSize) * viewportWidth;
        placeBoxConHeight = jQuery('.placeBox .placeContent').height();
        //newPadding = (topMenuPadding/initialViewportSize) * viewportWidth;
        jQuery('.sideMenu .menu ul li a span.icon img').css({'width':sidemenuIconSize, 'height':'auto'});
        console.log('placecontent ' +placeBoxConHeight);
        console.log('placeBoxHeight ' + placeBoxHeight);
        console.log('final ' + (placeBoxHeight-placeBoxConHeight-28));
        jQuery('.placeBox .placeContent').css({'margin-top':(placeBoxHeight-placeBoxConHeight-28)});
       // jQuery('#header .topLinks ul li.active a').css({'padding-left':newPadding, 'padding-right':newPadding});
    }else{
        jQuery('.sideMenu .menu ul li a span.icon img').css({'width':'', 'height':''});
        jQuery('.placeBox .placeContent').css({'margin-top':''});
       // jQuery('#header .topLinks ul li.active a').css({'padding-left':'', 'padding-right':''});
    }
});
/*
jQuery('.mainMenuLink').click(function() {
	jQuery('.sideMenu').toggleClass('sideMenu_hide');
	jQuery('#header').toggleClass('sideMenu_show_callback');
});

jQuery('.topLinks > ul li').click(function() {
	jQuery('.topLinks > ul li').removeClass('active');
	jQuery(this).addClass('active');
	idx = ($('.topLinks > ul li' ).index(this));
	idx++;
	jQuery('.page').removeClass('show').addClass('hide'); 
	jQuery('.page'+idx).addClass('show'); 
	 
});
	
*/
jQuery('body').delegate('*', 'touchstart',function(e)
{
    if ($(this) !== $altNav)
    {
        e.preventDefault();
        //and /or
        return false;
    }
    //current event target is $altNav, handle accordingly
});